﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Client.Service;
using ModelLayer;

namespace Client
{
    public partial class MovieSelect : Form
    {
        private IShow showService;
        private IBooking bookingService;
        private List<Show> list;
        Dictionary<Button, Show> buttons;

        private const int TEST_EMP_ID = 1111111114;
        private const float TEST_TOTAL_PRICE = 250.0f;
        private const bool TEST_IS_COLLECTED = false;

        public MovieSelect()
        {
            InitializeComponent();
            

            showService = new ShowClient("BasicHttpBinding_IShow");
            bookingService = new BookingClient("BasicHttpBinding_IBooking");

            list = showService.GetAllShows();
            buttons = new Dictionary<Button, Show>();

            
            DisplayShows();
            
        }

        private void DisplayShows()
        {
  
            foreach (Show s in list)
            {
                //PictureBoxSetup - create and set the properties of the image
                //LinkLabelSetup - create and set the properties of the linklabel
                //AssemblePanels - Assembles the controls in the TableLayoutPanel and FlowLayoutPanel respectively
                AssemblePanels(PictureBoxSetup(s), ButtonSetUp(s));
                
            }

            //add the FlowLayoutPanel to the window main panel
            movieSelectContainer.Controls.Add(panelMovieContainer);
        }

        /**
         * 
         * */
        private void FindDates()
        {
            testLabel.Text = list.Count.ToString();

            foreach (Show s in list)
            {

                comboDateSelect.Items.Add(s.Date.Day + "/" + s.Date.Month + "/" + s.Date.Year);

            }
        }
        /**
         * Create and set the properties of the image
         * */
        private PictureBox PictureBoxSetup(Show s)
        {
            PictureBox box = new PictureBox();
            box.ImageLocation = s.ImageDir;
            box.BorderStyle = BorderStyle.FixedSingle;
            box.SizeMode = PictureBoxSizeMode.AutoSize;
            //box.Dock = DockStyle.Fill;
 
            
            return box;
        }

        /**
         * Create and set the properties of the linklabel
         * also adds the button/show reference to the dictionary: buttons.
         * */
        private Button ButtonSetUp(Show s)
        {
            Button button = new Button();
            button.Click += new EventHandler(button_Click);
            button.Dock = DockStyle.Left;
            button.Text = s.Date.Date.ToShortTimeString();
            button.Name = "select" + s.Show_Id.ToString();
            button.TextAlign = ContentAlignment.TopCenter;

            buttons.Add(button, s);

            return button;
        }

        /**
         * Assembles the controls in the TableLayoutPanel and FlowLayoutPanel respectively
         * 
         * */
        private void AssemblePanels(PictureBox box, Button button)
        {
            

            //create table
            TableLayoutPanel table = new TableLayoutPanel();

            //adds the image and the label to the table
            table.Controls.Add(box, 0, 0);
            table.Controls.Add(button, 0, 1);

            //sets the size of the table to the size of the image + the size of the label
            //table.Height = box.Size.Height + label.Size.Height;
            //table.Width = box.Size.Width;
            table.Width = 145;
            table.Height = 300;
            table.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

            //add the TableLayoutPanel to the FlowLayoutPanel
            panelMovieContainer.Controls.Add(table);
        }

        private void comboDateSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = comboDateSelect.SelectedIndex;

            Object selectedItem = comboDateSelect.SelectedItem;

            MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "\n" +
                            "Index: " + selectedIndex.ToString());
        }


        void button_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;          //grab the button that caused the event
            Show s = buttons[b];                //retrieve the show associated with said buttton
            Booking booking = bookingService.CreateBooking(s.Show_Id, TEST_EMP_ID, TEST_TOTAL_PRICE, TEST_IS_COLLECTED);

            DisposeOpenGui();
            SearchCustomerGUI custGui = new SearchCustomerGUI();
            custGui.MdiParent = this.MdiParent;
            custGui.Show();
            custGui.WindowState = FormWindowState.Maximized;
        }

        private void DisposeOpenGui()
        {
            Form activeForm = ActiveMdiChild;
            if (activeForm != null)
            {
                ActiveMdiChild.Dispose();
            }
            else
            {
                Console.WriteLine("No Forms Open");
            }
        }

      
    }
}
